package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.CasePool;
import cn.fintecher.pangolin.common.enums.CaseType;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.*;
import cn.fintecher.pangolin.service.business.model.request.*;
import cn.fintecher.pangolin.service.business.model.response.CheckStringResponse;
import cn.fintecher.pangolin.service.business.model.response.FindCaseFollowRecordResponse;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.CommentRespository;
import cn.fintecher.pangolin.service.business.service.CaseFollowRecordService;
import io.swagger.annotations.*;
import org.apache.commons.collections4.IteratorUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

/**
 * @Auther: xiaqun
 * @Description: 催记相关接口
 * @Date: 18:15 2019/9/16
 */

@RestController
@RequestMapping("/api/caseFollowRecordController")
@Api(value = "催记相关接口", description = "催记相关接口")
public class CaseFollowRecordController extends BaseController {

    @Autowired
    private CaseFollowRecordService caseFollowRecordService;

    @Autowired
    private CommentRespository commentRespository;

    /**
     * 催记查询
     */
    @ApiOperation(value = "催记查询", notes = "催记查询")
    @GetMapping("/getAllCaseFollowRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindCaseFollowRecordResponse>> getAllCaseFollowRecord(FindCaseFollowRecordRequest request, Pageable pageable,
                                                                                     @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get all case follow record,{}", request);
        Page<FindCaseFollowRecordResponse> page = caseFollowRecordService.getAllCaseFollowRecord(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 删除催记
     */
    @ApiOperation(value = "删除催记", notes = "删除催记")
    @DeleteMapping("/deleteCaseFollowRecord")
    public ResponseEntity<Void> deleteCaseFollowRecord(@RequestParam @ApiParam(value = "主键ID", required = true) String id,
                                                       @RequestParam @ApiParam(value = "案件类型", required = true) CaseType caseType) {
        log.info("REST request to delete case follow record,{},{}", id, caseType);
        caseFollowRecordService.deleteCaseFollowRecord(id, caseType);
        return ResponseEntity.ok().build();
    }

    /**
     * @Author wangxiangdong
     * @Description 修改催记
     * @Date 2019/10/18 17:51
     **/
    @ApiOperation(value = "修改催记", notes = "修改催记")
    @PostMapping("/updateCaseFollowRecord")
    public ResponseEntity<CaseFollowupRecord> updateCaseFollowRecord(@RequestBody UpdateFollowUpRecordRequest request, @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add case follow record,{}", request);
        caseFollowRecordService.updateCaseFollowRecord(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加催记
     */
    @ApiOperation(value = "添加催记", notes = "添加催记")
    @PostMapping("/addCaseFollowRecord")
    public ResponseEntity<CaseFollowupRecord> addCaseFollowRecord(@RequestBody AddCaseFollowRecordRequest request,
                                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add case follow record,{}", request);
        CaseFollowupRecord record = caseFollowRecordService.addCaseFollowRecord(request, token);
        return ResponseEntity.ok().body(record);
    }

    /**
     * 查找记录查询
     */
    @ApiOperation(value = "查找记录查询", notes = "查找记录查询")
    @GetMapping("/getAllFindRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindRecord>> getAllFindRecord(FindRecordRequest request, Pageable pageable) {
        log.info("REST request to get all find record,{}", request);
        Page<FindRecord> page = caseFollowRecordService.getAllFindRecord(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 添加查找记录
     */
    @ApiOperation(value = "添加查找记录", notes = "添加查找记录")
    @PostMapping("/addFindRecord")
    public ResponseEntity<Void> addFindRecord(@RequestBody AddFindRecordRequest request,
                                              @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add find record,{}", request);
        caseFollowRecordService.addFindRecord(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 其他催记查询
     */
    @ApiOperation(value = "其他催记查询", notes = "其他催记查询")
    @GetMapping("/getAllOtherRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<OtherRecord>> getAllOtherRecord(FindOtherRecordRequest request, Pageable pageable) {
        log.info("REST request to get all other record,{}", request);
        Page<OtherRecord> page = caseFollowRecordService.getAllOtherRecord(request, pageable);
        return ResponseEntity.ok().body(page);
    }


    @ApiOperation(value = "工单回复", notes = "工单回复")
    @PostMapping("/replyOtherRecord")
    public ResponseEntity<Void> replyOtherRecord(@RequestBody ReplyRequest replyRequest, @RequestHeader(value = "X-UserToken") String token) {
        caseFollowRecordService.replyOtherRecord(replyRequest, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加其他催记
     */
    @ApiOperation(value = "添加其他催记", notes = "添加其他催记")
    @PostMapping("/addOtherRecord")
    public ResponseEntity<Void> addOtherRecord(@RequestBody AddOtherRecordRequest request,
                                               @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add other record,{}", request);
        caseFollowRecordService.addOtherRecord(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加标注
     */
    @ApiOperation(value = "添加标注", notes = "添加标注")
    @PostMapping("/addTag")
    public ResponseEntity<Void> addTag(@RequestBody AddTagRequest request,
                                       @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add tag,{}", request);
        caseFollowRecordService.addTag(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除标注
     */
    @ApiOperation(value = "删除标注", notes = "删除标注")
    @DeleteMapping("/deleteTag")
    public ResponseEntity<Void> deleteTag(@RequestParam @ApiParam(value = "主键ID", required = true) String id,
                                          @RequestParam @ApiParam(value = "案件ID", required = true) String caseId,
                                          @RequestParam @ApiParam(value = "案件类型", required = true) CaseType caseType) {
        log.info("REST request to delete tag,{},{},{}", id, caseId, caseType);
        caseFollowRecordService.deleteTag(id, caseId, caseType);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询标注
     */
    @ApiOperation(value = "查询标注", notes = "查询标注")
    @GetMapping("/getAllTag")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<Comment>> getAllTag(FindTagRequest request, Pageable pageable) {
        log.info("REST request to get all tag,{}", request);
        Page<Comment> page = caseFollowRecordService.getAllTag(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/getCommentByType")
    @ApiOperation(value = "查询评语/批注/备忘录", notes = "查询评语/批注/备忘录")
    public ResponseEntity<List<Comment>> getCommentByType(CommentRequest request) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.must(matchQuery("commentType", request.getCommentTypeList())).must(matchPhraseQuery("caseId", request.getCaseId()));
        Iterable<Comment> search = commentRespository.search(queryBuilder);
        List<Comment> list = new ArrayList<>();
        if (search.iterator().hasNext()) {
            list = IteratorUtils.toList(search.iterator());
            list.sort(Comparator.comparing(Comment::getOperatorTime));
        }
        return new ResponseEntity<>(list, HttpStatus.OK);
    }


    @GetMapping("/getDebtFollowRecord")
    @ApiOperation(value = "获取共债跟进记录", notes = "获取共债跟进记录")
    public ResponseEntity<List<FindCaseFollowRecordResponse>> getDebtFollowRecord(DebtMessageRequest debtMessageRequest,
                                                                                  @RequestHeader(value = "X-UserToken") String token) {
        List<FindCaseFollowRecordResponse> findCaseFollowRecordResponseList = caseFollowRecordService.getDebtFollowRecord(debtMessageRequest, token);
        return new ResponseEntity<>(findCaseFollowRecordResponseList, HttpStatus.OK);
    }

    /**
     * 敏感词校验
     */
    @ApiOperation(value = "敏感词校验", notes = "敏感词校验")
    @PostMapping("/checkString")
    public ResponseEntity<CheckStringResponse> checkString(@RequestBody CheckStringRequest request) {
        log.info("REST request to check string,{}", request);
        CheckStringResponse response = caseFollowRecordService.checkString(request);
        return ResponseEntity.ok().body(response);
    }

    /**
     * 查询委前催记
     */
    @ApiOperation(value = "查询委前催记", notes = "查询委前催记")
    @GetMapping("/getAllPreRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PreRecord>> getAllPreRecord(FindPreRecordRequest request, Pageable pageable) {
        log.info("REST request to get all pre record,{}", request);
        Page<PreRecord> page = caseFollowRecordService.getAllPreRecord(request, pageable);
        return ResponseEntity.ok().body(page);
    }
}